package com.satan.util;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @ClassName WebUtil
 * @Description TODO
 * @date 2023/10/3 20:14
 * @Version 1.0
 */

public class WebUtil {
    /**
     * 将字符串渲染到客户端
     * @param response HttpServletResponse对象
     * @param content 要写出的字符串内容
     * @throws IOException 如果写出响应时发生异常
     */
    public static void renderJson(HttpServletResponse response, String content) throws IOException {
        // 设置响应状态和头信息
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("utf-8");

        // 获取输出流并写出响应
        ServletOutputStream outputStream = response.getOutputStream();
        try {
            outputStream.write(content.getBytes());
            outputStream.flush();
        } finally {
            // 关闭输出流
            outputStream.close();
        }
    }

}
